const { book, bookImage, borrow } = require("../model/index")
const { Op } = require("sequelize");
class BookService{
    /**
     * 更新书籍图片路径信息
     * @param {封面路径} image 
     * @param {书籍id} id 
     */
    async updateImageUrlById(image,id) {
        try {
            await book.update({ image }, {
                where: {
                    id
                }
            })
        } catch (error) {
            console.log(error)
        }
    }
    /**
     * 提交发布书籍的信息
     * @param {书籍信息} bookInfo 
     * @returns 
     */
    async addbookInfo(bookInfo) {
        try {
            let res = await book.create({
                ...bookInfo
            })
            return res.dataValues
        } catch (error) {
            console.log(error)
        }
    }
    /**
     * 获取书籍列表数据
     * @param {偏移量} offset 
     * @param {数据个数} limit 
     * @returns 
     */
    async getAllBook() {
        let res = await book.findAll()
        return res
    }
     //删除书籍
    async deleteBookById(id) {
        try {
            await book.destroy({
                where: {
                    id
                }
            })
        } catch (error) {
            console.log(error)
        }
        
    }
    /**
     * 
     * @param {*} id 
     * @returns 
     */
    async getBookByName(keyValue) {
        if (!keyValue) {
            let res = await book.findAll()
            return res
        }
        let res = await book.findAll({
            where: {
                [Op.or]: {
                    book_name: {
                        [Op.substring]: keyValue,  
                    },
                    author: {
                        [Op.substring]: keyValue,  
                    }
                }
                
            }
        })
        return res
    }
    /**
     * 
     * @param {书籍id} id 
     * @returns 
     */
    async getBookById(id) {
        try {
            let res=await book.findAll({
                where: {
                    id
                }
            })
            return res
        } catch (error) {
            console.log(error)
        }
    }
    /**
     * 创建borrow记录
     * @param {*} bookid 
     * @param {*} owner 
     * @param {*} borrower 
     * @returns 
     */
    async AddBorrow(bookid, owner, borrower) {
        try {
            let res = await borrow.create({
                bookid,
                owner,
                borrower,
                create_time:new Date()
            })
            return res
        } catch (error) {
            console.log(error)
        }
    }
    //把书籍状态enable设置为false
    async ModifyStatus(id) {
        try {
            let res = await book.update({
                enable:false,
            }, {
                where: {
                    id
                }
            })
            return res
        } catch (error) {
            console.log(error)
        }
    }
    /**
     * 获取书籍封面信息
     * @param {} id 
     * @returns 
     */
    async getBookImageById(bookId) {
        try {
            let res=await bookImage.findAll({
                where: {
                    bookId
                }
            })
            return res[0]
        } catch (error) {
            console.log(error)
        }
    }
    //获取借阅表信息
    async FindBorrowList() {
        try {
            let res=await borrow.findAll({
                include: ({
                    model: book,
                })
            })
            return res
        } catch (error) {
            console.log(error)
        }
    }
    //获取全部借阅书籍
    async FindBorrowAllList() {
        try {
            let res = await borrow.findAll({
                include: ({
                    model: book,
                })
            })
            return res
        } catch (error) {
            console.log(error)
        }
    }
    
    async getBorrowByName(keyValue) {
        if (!keyValue) {
            let res = await borrow.findAll({
                include: ({
                    model: book,
                })
            })
            return res
        }
        let res = await borrow.findAll({
            include: ({
                model: book,
            }),
            where: {
                [Op.or]: {
                    borrower: {
                        [Op.substring]: keyValue,  
                    },
                    owner: {
                        [Op.substring]: keyValue,  
                    }
                } 
            }
        })
        return res
    }
}
module.exports=new BookService()